//
//  SLHomeHeadBigView.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/12.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit

class SLHomeHeadBigView: UIView {
    var timer:Timer?
    @IBOutlet weak var numL: UILabel!
    
    @IBOutlet weak var topLabel: UILabel!
    @IBOutlet weak var numbL: UILabel!
    @IBOutlet weak var descLabel: UILabel!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var buttonA: UIButton!
    @IBOutlet weak var msgView: UIView!
    @IBOutlet weak var agreementV: UIImageView!
    @IBOutlet weak var serviceV: UIImageView!
    
    var msgArr:[String] = []
    var index:Int = 0
    func startTime(){
        self.timer?.invalidate()
        self.timer = nil
        self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(3), target: self, selector: #selector(msgScrollAction), userInfo: nil, repeats: true)
        RunLoop.current.add(self.timer!, forMode: .common)
        
    }
    lazy var tableView:UITableView = {
        let v = create_tableView(supView: self.msgView) { make in
            make.left.right.top.bottom.equalTo(0)
        }
        v.delegate = self
        v.dataSource = self
        v.register(SLHomeMsgCell.self, forCellReuseIdentifier: "SLHomeMsgCell")
        v.isUserInteractionEnabled = false
        return v
    }()
    func setupUI(){

    }
    func setMsg(messageArr :[String]){
        self.msgArr = messageArr
        self.tableView.reloadData()
        self.startTime()
        msgView.layer.masksToBounds = true
        msgView.layer.cornerRadius = 10
    }
    @objc func msgScrollAction(){
        index+=1
        if index == self.msgArr.count {
            index = 0
        }
        if self.msgArr.count>0 {}else{return}
        self.tableView.scrollToRow(at: IndexPath(row: index, section: 0), at: .none, animated: true)
    }
}
extension SLHomeHeadBigView: UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SLHomeMsgCell", for: indexPath) as! SLHomeMsgCell
        cell.setMsg(msg: self.msgArr[indexPath.row])
//        cell.isSelected = indexPath.row == index
        return cell
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.msgArr.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        40
    }
    
}
